/**
 * Created by zhanglihe on 15/11/7.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'collection',
    'template',
    'model',
    'dialog',
    'datepicker'
], function ($, common, ready, Collection, template, Model, dialog, datepicker) {
    var index = function () {
        this.init();
    };
    index.prototype = {
        init: function () {
            var _this = this;
            $.ajax({
                url: _global.url.api + 'dualsolt/'+id,
                type: 'get',
                async: false,
                success: function(res) {
                    if(res.code == 0) {
                        _this.set('data', res.data);
                    }
                }
            });
            this.fetch();
            this.event();
        },
        fetch: function () {
            var _this = this;
            _this.i = 1;
            _this.j = 1;
            if(this.data.info.left_set){
                $.each(this.data.info.left_set,function(i,info){
                    var html = template('add-tpl_left',info);
                    $("#main-view").append(html);
                });
            }else{
                var html = template('add-tpl_left',_this);
                $("#main-view").append(html);
            }

            if(this.data.info.right_set){
                $.each(this.data.info.right_set,function(j,info){
                    var html = template('add-tpl_right',info);
                    $("#mainleft-view").append(html);
                });
            }else{
                var html = template('add-tpl_right',_this);
                $("#mainleft-view").append(html);
            }
            if(this.data.info.timer){
                $.each(this.data.info.timer,function(j,info){
                    var html = template('timer-tpl',info);
                    $("#top_main").append(html);
                });
            }else{
                var html = template('timer-tpl',_this);
                $("#top_main").append(html);
            }
            if(this.data.info.proportion_left){
                var html = template('proportion_left-tpl',this.data.info);
                $("#proportion_left").append(html);
            }else{
                var html = template('proportion_left-tpl',_this);
                $("#proportion_left").append(html);
            }
            if(this.data.info.proportion_right){
                var html = template('proportion_right-tpl',this.data.info);
                $("#proportion_right").append(html);
            }else{
                var html = template('proportion_right-tpl',_this);
                $("#proportion_right").append(html);
            }
            $('.long_date').on('click', function() {
                WdatePicker({
                    el:this.id,
                    dateFmt:'yyyy-MM-dd HH:mm:ss'
                });
            });
        },
        set: function($k, $v) {
            this[$k] = $v;
        },
        event: function () {
            this.data = {};
            var _this = this;console.log(this.data.info);
            //保存
            _this.i = 10;
            _this.j = 10;
            $("#add_table_left").on("click", function (e) {
                e.preventDefault();
                _this.i++;
                var html = template('add-tpl_left', _this);
                $("#main-view").append(html);
                var dom = new Array();
                $("#main-view").children('div').each(function () {
                    dom.push($(this).attr('id'));
                });
                if (dom.length > 9) {
                    $(".add_table_left").hide();
                    return false;
                }
                $(".add_table_left").show();
            });
            $("#add_table_right").on("click", function (e) {
                e.preventDefault();
                _this.j++;
                var html = template('add-tpl_right', _this);
                $("#mainleft-view").append(html);
                var dom = new Array();
                $("#mainleft-view").children('div').each(function () {
                    dom.push($(this).attr('id'));
                });
                if (dom.length > 9) {
                    $(".add_table_right").hide();
                    return false;
                }
                $(".add_table_right").show();
            });

            $('#btn_save').on('click', function (e) {
                _this.data = {};
                _this.data.id =id;
                _this.data.show =$("input[name='show']:checked").val();
                _this.data.end_time =$("input[name='end_time']").val();
                _this.data.top=$("input[name='top']").val();
                _this.data.right=$("input[name='right']").val();
                var titles_left =new Array();
                $("input[name='title_left']").each(function () {
                    if ($.trim($(this).val()) != '') {
                        titles_left.push($(this).val());
                    }
                });
                _this.data.titles_left = titles_left.join(",");
                if (_this.data.titles_left == "") {
                    Alert({msg: '请输入左槽活动标题', type: 'error'});
                    return false;
                }
                var titles_right =new Array();
                $("input[name='title_right']").each(function () {
                    if ($.trim($(this).val()) != '') {
                        titles_right.push($(this).val());
                    }
                });
                _this.data.titles_right = titles_right.join(",");
                if (_this.data.titles_right == "") {
                    Alert({msg: '请输入右槽活动标题', type: 'error'});
                    return false;
                }
                var imgs_left = new Array();
                $("input[name='imgs_left']").each(function () {
                    if ($.trim($(this).val()) != '') {
                        imgs_left.push($(this).val());
                    }
                });
                _this.data.imgs_left = imgs_left.join(",");
                if (_this.data.imgs_left == "") {
                    Alert({msg: '请上传左槽图片', type: 'error'});
                    return false;
                }
                var imgs_right = new Array();
                $("input[name='imgs_right']").each(function () {
                    if ($.trim($(this).val()) != '') {
                        imgs_right.push($(this).val());
                    }
                });
                _this.data.imgs_right = imgs_right.join(",");
                if (_this.data.imgs_right == "") {
                    Alert({msg: '请上传右槽图片', type: 'error'});
                    return false;
                }
                var urls_left = new Array();
                $("input[name='url_left']").each(function () {
                    urls_left.push($.trim($(this).val()));

                });
                _this.data.urls_left = urls_left.join(",");
                var urls_right = new Array();
                $("input[name='url_right']").each(function () {
                    urls_right.push($.trim($(this).val()));

                });
                _this.data.urls_right = urls_right.join(",");
                var left_width =new Array();
                $("input[name='left_width']").each(function () {
                    left_width.push($.trim($(this).val()));

                });
                _this.data.left_width = left_width.join(",");
                var left_hight =new Array();
                $("input[name='left_hight']").each(function () {
                    left_hight.push($.trim($(this).val()));

                });
                _this.data.left_hight = left_hight.join(",");
                var right_width =new Array();
                $("input[name='right_width']").each(function () {
                    right_width.push($.trim($(this).val()));

                });
                _this.data.right_width = right_width.join(",");
                var right_hight =new Array();
                $("input[name='right_hight']").each(function () {
                    right_hight.push($.trim($(this).val()));

                });
                _this.data.right_hight = right_hight.join(",");
                _this.data.proportion_left =  $("input[name='proportion_left']").val();
                _this.data.proportion_right = $("input[name='proportion_right']").val();
                var check = true;
                for (var i = 0; i < urls_left.length; i++) {
                    if (!urls_left[i]) {
                        Alert({msg: '左槽h5活动页url必填', type: 'error'});
                        check = false;
                        return false;
                    }
                }
                for (var i = 0; i < urls_right.length; i++) {
                    if (!urls_right[i]) {
                        Alert({msg: '右槽h5活动页url必填', type: 'error'});
                        check = false;
                        return false;
                    }
                }
                if (!check) {
                    return false;
                }
                console.log(_this.data);
                if (new Model({
                        url: _global.url.api + 'dualsolt',
                        data: _this.data
                    }).put()) {
                }
            });
            $('body').on('click', '.delete_table_left', function (e) {
                e.preventDefault();
                var item = $(this).attr("attr");
                $("#tile_num_left" + item).remove();
                var dom = new Array();
                $("#main-view").children('div').each(function () {
                    dom.push($(this).attr('id'));
                });
                if (dom.length < 2) {
                    $(".delete_table_left").hide();
                    return false;
                }
                $(".delete_table_left").show();
            });
            $('body').on('click', '.delete_table_right', function (e) {
                e.preventDefault();
                var item = $(this).attr("attr");
                $("#tile_num_right" + item).remove();
                var dom = new Array();
                $("#mainleft-view").children('div').each(function () {
                    dom.push($(this).attr('id'));
                });
                if (dom.length < 2) {
                    $(".delete_table_right").hide();
                    return false;
                }
                $(".delete_table_right").show();
            });
            //上传图片
            $('body').on('click', '.btn-upload_left', function (e) {
                e.preventDefault();
                var item = $(this).attr("attr");
                _this.upload_dialog = dialog({
                    content: template('upload-tpl', {}),
                    width: 550,
                    ok: function () {
                        var img = $("#upload-frame")[0].contentWindow.image.get();
                        img.item = item;
                        _this.imgPreview(img);
                    }, cancel: function () {
                    }
                });
                _this.upload_dialog.show();
            });
            $('body').on('click', '.btn-upload_right', function (e) {
                e.preventDefault();
                var item = $(this).attr("attr");
                _this.upload_dialog = dialog({
                    content: template('upload-tpl', {}),
                    width: 550,
                    ok: function () {
                        var img = $("#upload-frame")[0].contentWindow.image.get();
                        img.item = item;
                        _this.imgPreviews(img);
                    }, cancel: function () {
                    }
                });
                _this.upload_dialog.show();
            });


        },

        change: function (e) {
            this.data[$(e.target).attr('name')] = $(e.target).val();
        },

        imgPreview: function (img) {
            console.log('this.data', this.data);
            if (typeof img.id !== 'undefined') {
                this.data.img = img.id;
                $('#img-view_left' + img.item).html(template('img-tpl_left', img));
            }
        },
        imgPreviews: function (img) {
            console.log('this.data', this.data);
            if (typeof img.id !== 'undefined') {
                this.data.img = img.id;
                $('#img-view_right' + img.item).html(template('img-tpl_right', img));
            }
        }
    };

    new index();

});